package com.filldream.fastboot.common.stockUtils.bd;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;

/**
 * 百度股市通相关接口
 */
@Slf4j
public class BdUtil {

    /**
     * 获取自定义汇率
     * @param code  汇率代码
     * @return  返回结果
     */
    public static JSONObject getHl(String code){
        JSONObject result = new JSONObject();
        try{
            String json = cn.hutool.http.HttpUtil.get("https://finance.pae.baidu.com/vapi/v1/getquotation?group=huilv_minute&need_reverse_real=1&code="+code+"&finClientType=pc");
            result = JSONObject.parseObject(json);
        }catch (Exception e){
            log.error("获取自定义汇率{}失败", code, e);
            result.put("ResultCode",500);
        }
        return result;
    }


    /**
     * 获取美元黄金
     * @return
     */
    public static JSONObject getUsua(){
        JSONObject result = new JSONObject();
        try{
            String json = HttpUtil.get("https://finance.pae.baidu.com/vapi/v1/getquotation?all=1&srcid=5353&pointType=string&group=quotation_minute_us&market_type=us&new_Format=1&finClientType=pc&query=USAU&code=USAU");
            result = JSONObject.parseObject(json);
        }catch (Exception e){
            log.error("获取美元黄金失败", e);
            result.put("ResultCode",500);
        }
        return result;
    }

    /**
     * 获取美国十年期国债收益率-指数
     * @return
     */
    public static JSONObject getUS10Y(){
        JSONObject result = new JSONObject();
        try {
            String json = HttpUtil.get("https://api-ddc-wscn.awtmt.com/market/kline?prod_code=US10YR.OTC&tick_count=1&period_type=300&adjust_price_type=forward&fields=tick_at%2Copen_px%2Cclose_px%2Chigh_px%2Clow_px%2Cturnover_volume%2Cturnover_value%2Caverage_px%2Cpx_change%2Cpx_change_rate%2Cavg_px%2Cma2");
            result = JSONObject.parseObject(json);
        }catch (Exception e){
            log.error("获取US10Y指数失败", e);
            result.put("code",500);
        }
        return result;
    }

    /**
     * 获取中国30年期国债收益率指数
     * @return
     */
    public static JSONObject getCN30Y(){
        JSONObject result = new JSONObject();
        try {
            String json = HttpUtil.get("https://api-ddc-wscn.awtmt.com/market/kline?prod_code=CN30YR.OTC&tick_count=1&period_type=300&adjust_price_type=forward&fields=tick_at%2Copen_px%2Cclose_px%2Chigh_px%2Clow_px%2Cturnover_volume%2Cturnover_value%2Caverage_px%2Cpx_change%2Cpx_change_rate%2Cavg_px%2Cma2");
            result = JSONObject.parseObject(json);
        }catch (Exception e){
            log.error("获取US10Y指数失败", e);
            result.put("code",500);
        }
        return result;
    }

    /**
     * 获取百度热搜，和股票无关
     * @return
     */
    public static JSONObject getHotSearch(){
        JSONObject result = new JSONObject();
        try{
            String html = HttpUtil.get("https://top.baidu.com/board?tab=realtime");
            html = html.substring( html.indexOf("<!--s-data:") + 11, html.indexOf("-->") );
            JSONArray hotList = JSONObject.parseObject(html).getJSONObject("data").getJSONArray("cards").getJSONObject(0).getJSONArray("content");
            result.put( "hotList", hotList );
            result.put( "code", 200 );
        }catch (Exception e){
            log.error("获取百度热搜失败", e);
            result.put("code",500);
            result.put("msg","获取百度热搜失败:" + e.getMessage());
        }
        return result;
    }


}
